package com.example.recordingpen_datacollection.Utils.ThreadCache;

public class CacheSingleton {
    private static CacheSingleton instance = new CacheSingleton();

    private TimedCache<String, String>  cache=null; // 5秒过期时间

    private CacheSingleton() {
         // 私有构造方法，防止外部实例化
     }
     public static CacheSingleton getInstance() {
         if (instance == null) {
             instance = new CacheSingleton();
         }
        return instance;
    }

    public void setValue(String key1, String value1,long expirationTime){
        if (cache == null){
            setValue1(key1,value1,expirationTime);
            System.out.println("setValue1");
        } else {
            setValue2(key1,value1,expirationTime);
            System.out.println("setValue2");
        }
    }

    private void setValue1(String key1, String value1,long expirationTime){
        cache = new TimedCache<>(expirationTime*1000); // expirationTime秒过期时间
        cache.put(key1, value1);

    }

    private void setValue2(String key1, String value1,long expirationTime){
        cache.setExpirationTime(expirationTime*1000);
        cache.put(key1, value1);
    }

    public String getValue(String key1){
        if(cache == null){
            return null;
        }
        String value1 = cache.get(key1);
        return value1;
    }


}